package com.sunrise.visual.frame.common
{
	public class StackedUtil
	{
		private var data:Array;
		private var config:Object;
		
		private var animationEnabled:String;	//是否动画
		private var plot_type:String;	//柱图显示方式，CategorizedVertical：竖直方向	CategorizedHorizontal:水平方向
		private var dataLabelEnabled:String;	//是否在柱图上显示数据，true or false
		private var dataLabelPosition:String;	//柱图上显示数据位置
		private var format_data:String;	//保留小数位
		private var unit:String;	//单位
		private var titleEnabled:String;	//是否启用标题
		private var title:String;	//启用标题时，标题名称
		private var legendEnabled:String;	//是否启用图例
		private var customizedLegend:Array;	//自定义图例
		private var customizedLegendColor:Array;	//自定义图例颜色
		private var controlsLabelEnabled:String;	//是否显示单位标签
		private var controlsLabelText:String;	//单位标签文本
		private var format_Y1data:String;	//y轴坐标保留小数位
		private var x_axis_Position:String;	//x轴位置
		private var y_axis_Position:String;	//y轴位置
		private var start_index:int;	//开始下标
		private var end_index:int;	//结束下标
		private var seriesColor:Array;	//柱图颜色
		private var wdbm:String;	//维度编码
		private var zbbm:Array;	//指标编码
		private var zbmc:Array;	//指标名称
		
		public function StackedUtil(data:Array, config:Object)
		{
			this.data = data;
			this.config = config;
		}
		
		private function getXML():XML {
			var xml:XML = <anychart>
							<margin all="0" left={this.config.marginLeft || "0"} right={this.config.marginRight || "0"}/>
							<settings>
								<animation enabled={this.config.animationEnabled || "True"}/>
							</settings>
							<charts>
								<chart plot_type={this.plot_type}>
									<data_plot_settings default_series_type="Bar">
										<bar_series style="Silver" point_padding="0" group_padding="0.5">
											<label_settings enabled={this.dataLabelEnabled}>
												<background enabled="false"/>
												<position anchor={this.dataLabelPosition} valign="Center" halign="Center"/>
												<font family="Microsoft Yahei" color="White" bold="false">
													<effects>
														<drop_shadow enabled="true" distance="1" angle="45" blur_x="1.5" blur_y="1.5" strength="2" opacity="0.5"/>
													</effects>
												</font>
												<format>{this.config.labelFormat||"{%Value}{numDecimals:"}{this.format_data}{"}"}</format>
											</label_settings>
											<tooltip_settings enabled="True">
												<font family="Microsoft Yahei" bold="false" size="12"/>
												<format>{"{%Name}\n{%SeriesName}: {%YValue}{numDecimals:"}{this.format_data}{"}"}{this.unit}</format>
											</tooltip_settings>
										</bar_series>
										<line_series>
											<marker_settings enabled="True">
												<marker type="%MarkerType"/>
												<states>
													<hover>
														<marker type="Diamond"/>
													</hover>
												</states>
											</marker_settings>
											<tooltip_settings enabled="True">
												<font family="Microsoft Yahei" bold="false" size="12"/>
												<format>{"{%Name}\n{%SeriesName}: {%YValue}{numDecimals:"}{this.format_data}{"}"}{this.unit}</format>
											</tooltip_settings>
										</line_series>
									</data_plot_settings>
									<chart_settings>
										<chart_background enabled="false"/>
										<data_plot_background enabled="false"/>
										<title enabled={this.titleEnabled}>
											<font family="Microsoft Yahei" bold="False" size="16" color="white" />
											<text>{this.title}</text>
										</title>
										<controls>
											<label position="Fixed" anchor="RightTop" inside_dataplot="false" enabled={this.controlsLabelEnabled}>
												<font family="Microsoft Yahei" bold="False" size="12" color="white" />
												<text>{this.controlsLabelText}</text>
											</label>
										</controls>
										<axes>
											<x_axis position={this.x_axis_Position}>
												<title enabled="false"/>
												<labels>
													<font family="Microsoft Yahei" bold="false" size="12" color="white"/>
													</labels>
												<line color="white"/>
												<major_tickmark color="white"/>
												<major_grid enabled="false"/>
												<minor_grid enabled="false"/>
											</x_axis>
											<y_axis position={this.y_axis_Position}>
												<scale mode="Stacked" maximum_offset={this.config.maximumOffset||"0.1"}/>
												<title enabled="false"/>
												<labels>
													<font family="Microsoft Yahei" bold="false" size="12" color="white"/>
													<format>{"{%Value}{numDecimals:"}{this.format_Y1data}{"}"}</format>
												</labels>
												<line color="white"/>
												<major_tickmark color="white"/>
												<major_grid enabled="false"/>
												<minor_grid enabled="false"/>
											</y_axis>
											<extra>
												
											</extra>
										</axes>
										<legend enabled={this.config.legendEnabled || "False"} ignore_auto_item="true" align="center" position="Bottom" rows_padding="1" columns_padding="1">
											<font family="Microsoft Yahei" bold="False" size="12" color="white">
											</font>
											<background enabled="false"/>
											<title enabled="false"/>
										</legend>
									</chart_settings>
									<data>

									</data>
								</chart>
							</charts>
						</anychart>;
			return xml;
		}
		
		public function parseXML():XML {
			this.animationEnabled = this.config.animationEnabled || "false";
			this.plot_type = this.config.plot_type || "CategorizedHorizontal";
			this.dataLabelEnabled = this.config.dataLabelEnabled || "true";
			this.dataLabelPosition = this.config.dataLabelPosition || "Center";
			this.format_data = this.config.format_data || "1";
			this.unit = this.config.unit || "";
			this.titleEnabled = this.config.titleEnabled || "false";
			this.title = this.config.title || "";
			this.legendEnabled = this.config.legendEnabled || "false";
			this.customizedLegend = this.config.customizedLegend || [];
			this.customizedLegendColor = this.config.customizedLegendColor || [];
			this.controlsLabelEnabled = this.config.controlsLabelEnabled || "false";
			this.controlsLabelText = this.config.controlsLabelText || "";
			this.format_Y1data = this.config.format_Y1data || "0";
			this.x_axis_Position = this.config.x_axis_Position || "Normal";
			this.y_axis_Position = this.config.y_axis_Position || "Normal";
			this.start_index = this.config.start_index || 0;
			this.end_index = this.config.end_index || 0;
			this.seriesColor = this.config.seriesColor;
			this.wdbm = this.config.wdbm;
			this.zbbm = this.config.zbbm;
			this.zbmc = this.config.zbmc;
			
			var xml:XML = this.getXML();
			xml.charts[0].chart[0].data[0] = "";
			var xmlStr:String = "";
			
			for(var i:int = 0; i < this.zbbm.length; i++) {
				for(var j:int = 0; j< this.zbbm[i].length; j++) {
					xmlStr = '<series name="' + this.zbmc[i][j] + '" y_axis="y_axis_name_' + (i) + '" color="' + this.seriesColor[i][j] + '">';
					var t:int = this.start_index;
					var len:int = this.data.length - this.end_index;
					for(var k:int = t; k < len; k++){
						xmlStr += '<point name="' + this.data[k][wdbm] + '" y="' + this.data[k][zbbm[i][j]] + '"/>';
					}
					xmlStr += '</series>';
					xml.charts[0].chart[0].data[0].appendChild(new XML(xmlStr));
				}
			}
			var tempStr:String = "";
			for(i = 1; i < this.zbbm.length; i++) {
				tempStr = '<y_axis name="y_axis_name_' + (i) + '" enabled="false"><scale mode="Stacked"/></y_axis>';
				xml.charts[0].chart[0].chart_settings[0].axes[0].extra[0].appendChild(new XML(tempStr));
			}
			var legendStr:String = "";
			if(this.customizedLegend.length > 0) {
				legendStr = '<items>';
				for(i = 0; i < this.customizedLegend.length; i++) {
					legendStr += '<item><icon color="'+ this.customizedLegendColor[i] +'"/></item><item><text>' + this.customizedLegend[i] + '</text></item>';
				}
				legendStr += '</items>';
			}else {
				legendStr = '<items><item source="series"/></items>';
			}
			xml.charts[0].chart[0].chart_settings[0].legend[0].appendChild(new XML(legendStr));
			return xml;
		}
	}
}